<?php

namespace App\Models;

use App\Constant\Constant;
use App\Models\Hotel\Hotel;
use Carbon\Carbon;
use App\Models\BaseModel;
use App\Helpers\MoneyHelper;
use App\Format\BaseFormat;

/**
 * App\Models\Product
 *
 * @property int $id
 * @property int $entity_id 实体对象ID
 * @property int $type 类型：1酒店套餐
 * @property string $name 套餐名称
 * @property string $thumbnail 封面
 * @property string $price 价格
 * @property int $sort 排序
 * @property int $is_enable 是否上架：1是 0否
 * @property int $is_delete 是否删除：1是 0否
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @property-read mixed $faddress
 * @method static \Illuminate\Database\Eloquent\Builder|Product apiBaseQuery($whereParams = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Product bgBaseQuery($whereParams = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Product newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Product newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel paramsQuery($fields, $params = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Product query()
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereEntityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereIds($ids = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereIsDelete($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereIsEnable($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereThumbnail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Product extends BaseModel
{
    /**
     * Define table name
     *
     * @var string
     */
    protected $table = 'products';

    /**
     * Define a primary key
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * A property that cannot be batched.
     *
     * @var array
     */
    protected $guarded = ['id'];
    protected $hidden  = ['updated_at'];
    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        self::creating(function ($model) {
            $model->is_enable = 1;
        });
    }

    // 是否酒店套餐
    public function isHotelProduct()
    {
        return $this->type == Constant::ProductType_Hotel;
    }
    /*********************************************属性格式化***************************************************************/

    /*********************************************设置属性***************************************************************/

    /*********************************************模型操作***************************************************************/
    public function scopeBgBaseQuery($query, $whereParams = [])
    {
        $query->paramsQuery(['name', 'type'], $whereParams);

        $type = $whereParams['type'] ?? null;
        if ($type == Constant::ProductType_Hotel) {
            $query->with(['hotel']);
            $query->whereHas('hotel');
        }
    }

    public function scopeApiBaseQuery($query, $whereParams = [])
    {
    }

    public static function getForms($fields = [], $type = 'search', $width = '300px')
    {
        $forms = [
            'entity_id' => ['title' => '酒店', 'type' => 'select'],
            'name'      => ['title' => '名称', 'type' => 'text'],
            'thumbnail' => ['title' => '封面', 'type' => 'image'],
            'price'     => ['title' => '价格', 'type' => 'number'],
            'sort'      => ['title' => '排序', 'type' => 'number'],
            'type'      => ['type' => 'hidden']
        ];
        return self::handleForms($forms, $fields, $type, $width);
    }
    /*********************************************模型关联***************************************************************/
    // 关联酒店
    public function hotel()
    {
        return $this->belongsTo(Hotel::class, 'entity_id', 'id');
    }
}
